package leaderus.study;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author lihongliang
 *
 */
public class MyCloneableObj implements Cloneable{
	private Map<String,MyInfo> myInfos;
    private String myId;
    private Date validDate;
	public MyCloneableObj(Map<String, MyInfo> myInfos, String myId,
			Date validDate) {
		super();
		this.myInfos = myInfos;
		this.myId = myId;
		this.validDate = validDate;
	}
    
    public MyCloneableObj() {
		
	}

	public Map<String, MyInfo> getMyInfos() {
		return myInfos;
	}

	public void setMyInfos(Map<String, MyInfo> myInfos) {
		this.myInfos = myInfos;
	}

	public String getMyId() {
		return myId;
	}

	public void setMyId(String myId) {
		this.myId = myId;
	}

	public Date getValidDate() {
		return validDate;
	}

	public void setValidDate(Date validDate) {
		this.validDate = validDate;
	}

	@Override
    public Object clone(){
    	MyCloneableObj o=null;
    	try {
			o=(MyCloneableObj)super.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	HashMap<String,MyInfo> myInfoMap = new HashMap<String,MyInfo>(o.myInfos.size());
    	Iterator<String> iterator = o.myInfos.keySet().iterator();
    	String key;
    	while(iterator.hasNext()){
    		key=String.valueOf(iterator.next());
    		MyInfo newMyInfo=(MyInfo)o.myInfos.get(key).clone();
    		myInfoMap.put(key, newMyInfo);
    	}

    	o.setMyInfos(myInfoMap);
    	return o;
    }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((myId == null) ? 0 : myId.hashCode());
		result = prime * result + ((myInfos == null) ? 0 : myInfos.hashCode());
		result = prime * result
				+ ((validDate == null) ? 0 : validDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MyCloneableObj other = (MyCloneableObj) obj;
		if (myId == null) {
			if (other.myId != null)
				return false;
		} else if (!myId.equals(other.myId))
			return false;
		if (myInfos == null) {
			if (other.myInfos != null)
				return false;
		} else if (!myInfos.equals(other.myInfos))
			return false;
		if (validDate == null) {
			if (other.validDate != null)
				return false;
		} else if (!validDate.equals(other.validDate))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "MyCloneableObj [myInfos=" + myInfos + ", myId=" + myId
				+ ", validDate=" + validDate + "]";
	}
}
